package com.tl.sun.ui.interfaces;

import android.content.Context;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.tl.sun.AppContext;
import com.tl.sun.OkHeader;
import com.tl.sun.manager.MDAccountMgr;
import com.tl.sun.utils.DeviceUuidFactory;
import com.tl.sun.utils.common.MD5Util;

import org.json.JSONObject;

import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by apple on 17/4/11.
 */

public class LoginPresent {
    private LoginInterface mCallBack;
    private Context mContext;

    public LoginPresent(LoginInterface callBack, Context context) {
        mCallBack = callBack;
        this.mContext = context;
    }

    public void login(String name, String password) {
        HashMap<String, String> map = new HashMap<>();
        map.put("username", name);
        map.put("password", password);
        map.put("device_token", MDAccountMgr.getInstance().getDeviceToken());
        map.put("device_token", MDAccountMgr.getInstance().getDeviceToken());
        map.put("client_id", MD5Util.md5(new DeviceUuidFactory(AppContext.getInstance()).getDeviceUuid().toString()));
        JSONObject jsonObject = new JSONObject(map);

        OkGo.getInstance().addCommonHeaders(OkHeader.getCommonHead(mContext)).post(AppContext.getInstance().getBaseUrl() + "account/login_v2")
                .tag(this)
                .upJson(jsonObject.toString())
                .execute(new StringCallback() {
                             @Override
                             public void onSuccess(String s, Call call, Response response) {
                                 mCallBack.onSuccess(s);
                             }

                             @Override
                             public void onError(Call call, Response response, Exception e) {
                                 mCallBack.onError();
                             }
                         }

                );
    }
}
